package com.ruoyi.gcgl.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.gcgl.domain.GcProjectSbclht;
import com.ruoyi.gcgl.domain.GcProjectSbclhtFj;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.domain.GcProjectSgazhtFj;
import com.ruoyi.gcgl.mapper.GcProjectSgazhtFjMapper;
import com.ruoyi.gcgl.mapper.GcProjectSgazhtMapper;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;

/**
 * 施工安装合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-29
 */
@Service
public class GcProjectSgazhtServiceImpl implements IGcProjectSgazhtService 
{
    @Autowired
    private GcProjectSgazhtMapper gcProjectSgazhtMapper;
    @Autowired
    private GcProjectSgazhtFjMapper gcProjectSgazhtFjMapper;

    /**
     * 查询施工安装合同
     * 
     * @param id 施工安装合同主键
     * @return 施工安装合同
     */
    @Override
    public GcProjectSgazht selectGcProjectSgazhtById(Long id){
    	GcProjectSgazht obj = gcProjectSgazhtMapper.selectGcProjectSgazhtById(id);
    	
    	GcProjectSgazhtFj sgazhtFj = new GcProjectSgazhtFj();
    	sgazhtFj.setDeleteFlag(1);
    	sgazhtFj.setType("1");
    	sgazhtFj.setSgazhtId(id);
    	List<GcProjectSgazhtFj> fj = gcProjectSgazhtFjMapper.selectGcProjectSgazhtFjList(sgazhtFj);
    	
    	if(fj!=null && fj.size()>0){
    		obj.setZlwj(fj.get(0).getZilwj());
    	}
    	
    	GcProjectSgazhtFj htsgazhtFj = new GcProjectSgazhtFj();
    	htsgazhtFj.setDeleteFlag(1);
    	htsgazhtFj.setType("3");
    	htsgazhtFj.setSgazhtId(id);
    	List<GcProjectSgazhtFj> htfj = gcProjectSgazhtFjMapper.selectGcProjectSgazhtFjList(htsgazhtFj);
    	
    	if(htfj!=null && htfj.size()>0){
    		obj.setHzzszlwj(htfj.get(0).getZilwj());
    	}
    	
        return obj;
    }

    /**
     * 查询施工安装合同列表
     * 
     * @param gcProjectSgazht 施工安装合同
     * @return 施工安装合同
     */
    @Override
    public List<GcProjectSgazht> selectGcProjectSgazhtList(GcProjectSgazht gcProjectSgazht)
    {
        return gcProjectSgazhtMapper.selectGcProjectSgazhtList(gcProjectSgazht);
    }

    /**
     * 新增施工安装合同
     * 
     * @param gcProjectSgazht 施工安装合同
     * @return 结果
     */
    @Override
    public int insertGcProjectSgazht(GcProjectSgazht gcProjectSgazht){
    	gcProjectSgazhtMapper.insertGcProjectSgazht(gcProjectSgazht);
    	
    	GcProjectSgazhtFj gcProjectSgazhtFj = new GcProjectSgazhtFj();
    	gcProjectSgazhtFj.setDeleteFlag(1);
    	gcProjectSgazhtFj.setCreateAt(new Date());
    	gcProjectSgazhtFj.setUpdateAt(new Date());
    	gcProjectSgazhtFj.setGcProjectid(gcProjectSgazht.getGcProjectid());
    	gcProjectSgazhtFj.setOrderNo(gcProjectSgazht.getOrderNo());
    	gcProjectSgazhtFj.setSgazhtId(gcProjectSgazht.getId());
    	gcProjectSgazhtFj.setType("1");//1合同，2询价单
    	gcProjectSgazhtFj.setZilwj(gcProjectSgazht.getZlwj());
    	gcProjectSgazhtFjMapper.insertGcProjectSgazhtFj(gcProjectSgazhtFj);
    	
        return 1;
    }

    /**
     * 修改施工安装合同
     * 
     * @param gcProjectSgazht 施工安装合同
     * @return 结果
     */
    @Override
    public int updateGcProjectSgazht(GcProjectSgazht gcProjectSgazht){
    	GcProjectSgazhtFj gcProjectSgazhtFj = new GcProjectSgazhtFj();
    	gcProjectSgazhtFj.setDeleteFlag(1);
    	gcProjectSgazhtFj.setGcProjectid(gcProjectSgazht.getGcProjectid());
    	gcProjectSgazhtFj.setOrderNo(gcProjectSgazht.getOrderNo());
    	gcProjectSgazhtFj.setSgazhtId(gcProjectSgazht.getId());
    	gcProjectSgazhtFj.setType("1");//1合同，2询价单
    	List<GcProjectSgazhtFj> lis = gcProjectSgazhtFjMapper.selectGcProjectSgazhtFjList(gcProjectSgazhtFj);
    	
    	if(lis!=null && lis.size()>0){
    		GcProjectSgazhtFj obj = lis.get(0);
        	obj.setZilwj(gcProjectSgazht.getZlwj());
        	gcProjectSgazhtFjMapper.updateGcProjectSgazhtFj(obj);
    	}else{
        	gcProjectSgazhtFj.setCreateAt(new Date());
        	gcProjectSgazhtFj.setUpdateAt(new Date());
        	gcProjectSgazhtFj.setZilwj(gcProjectSgazht.getZlwj());
        	gcProjectSgazhtFjMapper.insertGcProjectSgazhtFj(gcProjectSgazhtFj);
    	}
    	
        return gcProjectSgazhtMapper.updateGcProjectSgazht(gcProjectSgazht);
    }

    /**
     * 批量删除施工安装合同
     * 
     * @param ids 需要删除的施工安装合同主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectSgazhtByIds(String ids)
    {
        return gcProjectSgazhtMapper.deleteGcProjectSgazhtByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除施工安装合同信息
     * 
     * @param id 施工安装合同主键
     * @return 结果
     */
    @Override
    public int deleteGcProjectSgazhtById(Long id)
    {
        return gcProjectSgazhtMapper.deleteGcProjectSgazhtById(id);
    }
}
